package com.asyf.demo.controller;

import com.asyf.demo.utils.CRJavaHelper;
import com.crystaldecisions.sdk.occa.report.application.ReportClientDocument;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.DateFormat;
import java.util.Date;

@Controller
public class DemoController {

    //jsp测试
    @RequestMapping("/index")
    public String hello(Model m) {
        m.addAttribute("now", DateFormat.getDateTimeInstance().format(new Date()));
        //视图重定向index.jsp
        return "index";
    }

    //预览空报表
    @GetMapping("/cr")
    public String cr(Model model) {
        return "cr";
    }

    //直接使用jsp预览水晶报表
    @GetMapping("/cr2")
    public String cr2(Model model) {
        return "cr2";
    }

    //公共jsp文档，将水晶报表的设置放到后端
    @GetMapping("/cr3")
    public String cr3(Model model, HttpServletRequest request) throws Exception {
        // 水晶报表的位置
        final String REPORT_NAME = "/WEB-INF/test2.rpt";
        //打开报表
        ReportClientDocument reportClientDoc = new ReportClientDocument();
        reportClientDoc.open(REPORT_NAME, 0);

        //更换数据源
        String username = "scott";
        String password = "root";
        String driverName = "oracle.jdbc.OracleDriver";
        String connectionURL = "jdbc:oracle:thin:@localhost:1521:orcl";
        String jndiName = "JDBC(JNDI)";
        CRJavaHelper.changeDataSource(reportClientDoc, username, password, connectionURL, driverName, jndiName);

        //给参数赋值
        //第二个参数代表子报表，设置主报表参数，设置为空串
        CRJavaHelper.addDiscreteParameterValue(reportClientDoc, "", "deptNoPar", 10);
        //C:\\Users\\Administrator\\Desktop\\001.jpg"
        CRJavaHelper.addDiscreteParameterValue(reportClientDoc, "", "imagePath", "");

        // 把报表源放进session,传递到报表显示页面
        HttpSession session = request.getSession();
        session.setAttribute("reportSource", reportClientDoc.getReportSource());
        return "cr3";
    }
}
